﻿using RXY_Mall.Application.Manager;
using System.Threading.Tasks;
using System.Web.Mvc;
using RXY_Mall.Application.Manager.Dto;
using RXY_Mall.Application.Statistics;

namespace RXY_Mall.Admin.Controllers
{
    public class ManagerController : BaseController
    {
        private IManagerService _ManagerService;
        private IStatisticsService _statisticsService;
        public ManagerController()
        {
            _ManagerService = new ManagerService();
            _statisticsService = new StatisticsService();
        }

        public ActionResult Create()
        {
            ManagerAddInput input = new ManagerAddInput();
            return View(input);
        }

        [HttpPost]
        public async Task<ActionResult> Create(ManagerAddInput input)
        {
            var result = await _ManagerService.AddManager(input);
            return View();
        }

        [HttpGet]
        public ActionResult List()
        {
            ViewBag.SetRole = 0;
            //if (MemberId==1||MemberId==47)
            //{
            //    ViewBag.SetRole = 1;
            //}            
            var statistics = _statisticsService.GetManagerStatistics();
            if (statistics == null)
            {
                ViewBag.Manager_total = 0;
                ViewBag.Manager_today = 0;
            }
            else
            {
                ViewBag.Manager_total = statistics.user_total;
                ViewBag.Manager_today = statistics.user_today;
            }
            return View();
        }

        public JsonResult GetManagersByPage(int iDisplayStart, int iDisplayLength, string start_date, string end_date, string manager_name, string manager_no, int status)
        {
            var result = _ManagerService.GetManagersByPage(start_date, end_date, manager_name, manager_no, status, iDisplayStart / iDisplayLength + 1, iDisplayLength);
            if (result.code != 1)
            {
                ErrorNotification(result.msg);
            }
            return Json(new { iTotalRecords = result.data.TotalCount, iTotalDisplayRecords = result.data.TotalCount, aaData = result.data }, JsonRequestBehavior.AllowGet);

        }

        //重置密码  
        [HttpGet]
        public ActionResult SetPwd(int id)
        {
            if (id <= 0)
            {
                return Json(new { status = "n", info = "参数错误。" });
            }
            //var result = ManagerService.SetManagerPwd(MemberId, id, "e10adc3949ba59abbe56e057f20f883e");
            var result = _ManagerService.SetManagerPwd(MemberId, id, "123456");
            if (result)
            {
                return Json(new { status = "y", info = "操作成功。" });
            }
            else
            {
                return Json(new { status = "n", info = "操作失败。" });
            }
        }

        //禁用/启用
        [HttpPost]
        public ActionResult SetIsUse()
        {
            string id = Request.Form["id"];
            string isUse = Request.Form["isUse"];
            int idDefault = 0;
            int isUseDefault = 0;
            if (string.IsNullOrWhiteSpace(id) || !int.TryParse(id, out idDefault))
            {
                return Json(new { status = "n", info = "ID错误" });
            }
            if (string.IsNullOrWhiteSpace(isUse) || !int.TryParse(isUse, out isUseDefault))
            {
                return Json(new { status = "n", info = "参数错误" });
            }

            var result = _ManagerService.SetIsUse(MemberId, idDefault, isUseDefault);
            if (result)
            {
                return Json(new { status = "y", info = "操作成功。" });
            }
            else
            {
                return Json(new { status = "n", info = "操作失败。" });
            }
        }
    }
}